package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "defaultHandler")
public class OrderHystrixController {

    @Resource
    private PaymentService paymentService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable Integer id){
        return paymentService.paymentInfoOK(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
    })
    public String paymentInfoTimeout(@PathVariable Integer id){
        return paymentService.paymentInfoTimeout(id);
    }

    public String paymentInfoTimeoutHandler(Integer id){
        return "消费者等待超时";
    }

    @GetMapping("/consumer/payment/hystrix/defayktTimeout/{id}")
    @HystrixCommand
    public String defayktTimeout(@PathVariable Integer id){
        return paymentService.paymentInfoTimeout(id);
    }


    @GetMapping("/consumer/payment/hystrix/feignFallback/{id}")
    public String feignFallback(@PathVariable Integer id){
        return paymentService.paymentInfoTimeout(id);
    }

    public String defaultHandler(){
        return "出现一点小问题,请稍候再试。";
    }
}
